// pages/bar_chart/bar_chart.js
import * as echarts from '../../ec-canvas/echarts';
const app = getApp();
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // 像素
  });
  canvas.setChart(chart);

  var option = {
    backgroundColor: '#ffffff',
    title:{
      text:'堆叠条形图'
    },
    legend: {
      type: 'scroll',
      top: 'top'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {         
        type: 'shadow'      
      }
    },
    dataZoom:[
      {
        type: 'slider',
        show: true,
        xAxisIndex: [0],
        start: 0,
        end: 100,
      },{
        type: 'slider',
        show: true,
        yAxisIndex: [0],
        start: 0,
        end: 100
      },{
        type: 'inside',
        xAxisIndex: [0],
        start: 0,
        end: 100
      },{
        type: 'inside',
        yAxisIndex: [0],
        start: 0,
        end: 100
      }
    ],
    xAxis: {
      type: 'value',
    },
    yAxis: {
      type: 'category',
      axisLabel: (function (){
        if(app.globalData.data2[0].length > 4){
          var Arr = {
            interval:0,  
            rotate:40 
          }
        }else{
          var Arr = {};
        }
        return Arr;
      })(),
      data: (function (){
        if(app.globalData.data1 != []){
          var arr = app.globalData.data1
        }else{
          var a = 0
          var arr = []
          while(a < app.globalData.data2[0].length){
            arr.push(a+1)
            a++
          }
        }
        return arr;
      })()
    },
    series: (function (){
      var arr = [];
      var b = 0;
      if(app.globalData.data2[0].length < 10){
        while(b < app.globalData.data2.length){
          arr.push({
            name: b+1,
            type: 'bar',
            stack: 'total',
            label : {
              show: true,  //重点
              color:'#000', //颜色
              position: 'inside'  //位置
            },
            data: app.globalData.data2[b]
          });
          b++;
        }
      }else{
        while(b < app.globalData.data2.length){
          arr.push({
            name: b+1,
            type: 'bar',
            stack: 'total',
            data: app.globalData.data2[b]
          });
          b++;
        }
      }
      return arr;
    })()
  };
  chart.setOption(option);
  return chart;
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      onInit: initChart
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  save_path(){
    setTimeout(() => {
      this.save();
    }, 1000);
  },

  save() {
    const ecComponent = this.selectComponent('#mychart-dom-bar');
    // 先保存图片到临时的本地文件，然后存入系统相册
    ecComponent.canvasToTempFilePath({
      success: res => {
        //console.log("tempFilePath:", res.tempFilePath)
        // 存入系统相册
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath || '',
          success: res => {
            //console.log("success", res)
            wx.showToast({
              title: '图片保存成功',
              icon: 'success'
            })
          },
          fail: res => {
            //console.log("fail", res)
            wx.showToast({
              title: '图片保存失败',
              icon: 'error'
            })
          }
        })
      },
      fail: res => console.log(res)
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})